/**
 *  texvalue 搜索内容    marketId 商场ID
 *  brandUrl 品牌请求地址  pruductsUrl 商品请求地址 requestMain 请求主体
 * */
angular.module('myApp', ['infinite-scroll']);
var texvalue = "";
var val = document.getElementById("tex");
var marketId = readData(localStorage.marketId);
var brandUrl = getUrl("91shoppeSearch");
var pruductsUrl = getUrl("92productSearch");
var requestMain = {};

function productSearch($scope, $http) {
	$scope.pages = 0; //默认加载第一页，初始值0	
	$scope.goodsAll = []; //所有商品
	$scope.totalpage = 1; //默认总页码数	
	$scope.brandlist = function() { //品牌专柜	
		texvalue = val.value;
		$scope.loginJudge();
		plus.nativeUI.showWaiting('加载中...');
		$http(requestMain1).success(function(data, status, headers, config) {
			plus.nativeUI.closeWaiting();
			console.log('品牌专柜' + JSON.stringify(data))
			if (data.obj.shoppes.length > 0) {
				$scope.pinpai = true;
				$scope.brands = [];
				var brandsList = data.obj.shoppes;
				for (var i = 0; i < brandsList.length; i++) {
					$scope.brands.push(brandsList[i]);
				}
				$scope.search();
			} else {
				$scope.pinpai = false;
				$scope.brands = [];
				mui.toast('无此品牌商品');
			};
		}).error(function(er) {
			plus.nativeUI.closeWaiting();
			console.log("品牌专柜" + er);
		});
	}
	$scope.search = function() { //搜索加载商品		
		$scope.pages = 0; //默认加载第一页，初始值0	
		$scope.goodsAll = []; //所有商品
		$scope.totalpage = 1; //默认总页码数	
		$scope.goodsAll = [];
		$http(requestMain2).success(function(data, status, headers, config) {
			//alert(JSON.stringify(data))
			if (data.obj) {
				var goodsList = data.obj.products;
				for (var i = 0; i < goodsList.length; i++) {
					$scope.goodsAll.push(goodsList[i]);
				}
				if ($scope.goodsAll.length > 0) {
					$scope.pro = true;
				} else {
					$scope.pro = false;
				}
				$scope.totalpage = data.obj.totalCount;
				$scope.pages = $scope.pages + 1;
				setTimeout(function() {
					echo.init({
						offset: 0,
						throttle: 0
					});
				}, 0);
			} else {
				$scope.goodsAll = [];
				//mui.toast('无此商品');
			};
		}).error(function(e) {
			console.log("商品搜索网络连接异常" + e);
		});
	}
	$scope.loadMore = function() { //上拉加载
		if (texvalue == "")
			return;
		if ($scope.pages < $scope.totalpage) {
			$scope.loginJudge();
			$scope.pages += 1;
			$http(requestMain2).success(function(data, status, headers, config) {
				if (data.obj) {
					console.log(JSON.stringify(requestMain2) + JSON.stringify($scope.goodsAll))
					var goodsList = data.obj.products;
					for (var i = 0; i < goodsList.length; i++) {
						$scope.goodsAll.push(goodsList[i]);
					}

					setTimeout(function() {
						echo.init({
							offset: '100px',
							throttle: 0
						});
					}, 0);
					$scope.totalpage = data.obj.totalCount / 6;

				} else {
					$scope.goodsAll = [];
					$scope.pages = $scope.pages - 1;
				};
			}).error(function() {
				console.log("网络连接异常，请稍后重试(e)");
				$scope.pages = $scope.pages - 1;
			});
		}
	}
	$scope.detail = function(alls, type) {
		if (getlogin('../my/loginHead.html')) {
			if (alls.circle.attention) {
				if (type == 1) { //打开圈子页
					openCircle({
						urls: 'circleMain.html',
						circleId: alls.circle.circleId,
						guideId: alls.circle.userId,
						circleName: alls.circle.circleName,
						groupId: alls.circle.noticeGroupId
					});
				} else { //打开商品详情页
					openProductDetail({
						urls: 'productDetail.html',
						goodId: alls.productId,
						guideId: alls.circle.userId,
						circleName: alls.circle.circleName,
						groupId: alls.circle.noticeGroupId
					});
				}
			} else { //打开圈子详情页
				openGuanzhu({
					uid: alls.circle.userId,
					urls: 'guanzhu.html'
				});
			}
		}
	}
	$scope.loginJudge = function() {
		if (localStorage.account) { //已登录
			brandUrl = getUrl("91shoppeSearch");
			var headdata1 = getHeader();
			var aid = readData(localStorage.account);
			requestMain1 = {
				method: 'POST',
				url: brandUrl,
				headers: headdata1,
				timeout: 10000,
				params: {
					marketId: marketId,
					accountId: aid,
					search: texvalue
				}
			};
			pruductsUrl = getUrl("92productSearch");
			var headdata2 = getHeader();
			requestMain2 = {
				method: 'POST',
				url: pruductsUrl,
				headers: headdata2,
				timeout: 10000,
				params: {
					marketId: marketId,
					accountId: aid,
					search: texvalue,
					pageIndex: $scope.pages,
					pageSize: 6
				}
			};
		} else { //未登录
			requestMain1 = {
				method: 'POST',
				url: brandUrl,
				timeout: 10000,
				params: {
					marketId: marketId,
					search: texvalue
				}
			};
			requestMain2 = {
				method: 'POST',
				url: pruductsUrl,
				timeout: 10000,
				params: {
					marketId: marketId,
					search: texvalue,
					pageIndex: $scope.pages,
					pageSize: 6
				}
			};
		}
	}
	$scope.openGuan = function(uid) {
		event.stopPropagation();
		openGuanzhu({
			urls: "guanzhu.html",
			uid: uid
		})
	}
}